﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.Stock
{	public partial class RepriceLine : Form
	{	private	Util.ActionType actionType;
//-------------------------------------------------------------------------------------------------
		public	DIProject.DataSets.Dictionary.PartiesRow PartyRow{get{return partyRow;}}
		private	DIProject.DataSets.Dictionary.PartiesRow partyRow = null;
//-------------------------------------------------------------------------------------------------
		public	int? Party
		{	get{return partyRow != null? (int?)partyRow.Id: null;}
			set
			{	if(actionType != Util.ActionType.Insert)
					party = value;
			}
		}
		private	int? party = null;

		public	string Manufacturer
		{	get{return textBoxManufacturer.Text;}
			set
			{	if(actionType != Util.ActionType.Insert)
					textBoxManufacturer.Text = value;
			}
		}

		public	int Qntty
		{	get{return (int)numericUpDownQntty.Value;}
			set
			{	if(actionType != Util.ActionType.Insert)
					numericUpDownQntty.Value = qntty = value;
			}
		}
		private	int qntty = 0;

		public	decimal OldPrice
		{	get{return oldPrice;}
			set
			{	if(actionType != Util.ActionType.Insert)
				{	oldPrice			= value;
					textBoxPrice.Text	= oldPrice.ToString("N2");
				}
			}
		}
		private	decimal oldPrice = 0m;

		public	decimal NewPrice
		{	get{return numericUpDownPrice.Value;}
			set
			{	if(actionType != Util.ActionType.Insert)
					numericUpDownPrice.Value = value;
			}
		}

		public	int ReasonId
		{	get{return (int)comboBoxReasonName.SelectedValue;}
			set
			{	if(actionType != Util.ActionType.Insert)
					reasonId = value;
			}
		}
		private	int reasonId = 0;

		public	string ReasonName
		{	get{return comboBoxReasonName.Text;}
		}

		public	string ReasonText
		{	get{return textBoxReasonText.Text;}
			set{textBoxReasonText.Text = value;}
		}

		public	string QualityLossMark
		{	get{return textBoxQualityLossMark.Text;}
			set{textBoxQualityLossMark.Text = value;}
		}

		public	string Note
		{	get{return textBoxNote.Text;}
			set{textBoxNote.Text = value;}
		}
//-------------------------------------------------------------------------------------------------
		private void UpdateControls()
		{	if(partyRow != null)
			{	oldPrice					= partyRow.RetailPrice;
				textBoxName.Text			= partyRow.Name;
				textBoxManufacturer.Text	= partyRow.ManufacturerName;
				textBoxPrice.Text			= oldPrice.ToString("N2");
				if(actionType == DIProject.Util.ActionType.Insert)
					numericUpDownPrice.Value= oldPrice;
				if(Qntty > partyRow.Qntty + qntty)
					numericUpDownQntty.Value= partyRow.Qntty + qntty;
				numericUpDownQntty.Maximum	= partyRow.Qntty + qntty;
				buttonAccept.Enabled		= true;
			}
			else
			{	numericUpDownQntty.Maximum	= int.MaxValue;
				buttonAccept.Enabled		= false;
			}
		}
//-------------------------------------------------------------------------------------------------
		public RepriceLine() : this(Util.ActionType.NoAction)
		{
		}
		public RepriceLine(Util.ActionType actionType)
		{	InitializeComponent();
			numericUpDownQntty.Minimum	= 1;
			numericUpDownQntty.Maximum	= int.MaxValue;
			numericUpDownPrice.Minimum	= 0m;
			numericUpDownPrice.Maximum	= decimal.MaxValue;
			buttonAccept.Enabled		= false;
			this.actionType	= actionType;
		}
//-------------------------------------------------------------------------------------------------
		private void RepriceLine_Load(object sender, EventArgs e)
		{	try
			{	if(actionType != DIProject.Util.ActionType.Insert)
				{	partiesTableAdapter.Fill(dictionary.Parties, party, true, null, null);
					if(dictionary.Parties.Count > 0)
					{	partyRow = dictionary.Parties[0];
						UpdateControls();
					}
					buttonParty.Enabled = false;
					ActiveControl = numericUpDownPrice;
					Text = Properties.Resources.strUpdateDocLine;
				}
				else
					Text = Properties.Resources.strInsertDocLine;
				repriceWriteoffReasonTableAdapter.Fill(this.dictionary.RepriceWriteoffReason);
				if(actionType != Util.ActionType.Insert)
					comboBoxReasonName.SelectedValue = reasonId;
			}
			catch(SqlException eq)
			{	MessageBox.Show(this, string.Format("{0} ({1}): {2}", eq.Procedure, eq.LineNumber, eq.Message), Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			catch(Exception ex)
			{	MessageBox.Show(this, ex.Message, Properties.Resources.strError, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}
//-------------------------------------------------------------------------------------------------
		private void buttonParty_Click(object sender, EventArgs e)
		{	PartyChoice partyChoice = new PartyChoice(Party, true, null, null);
			if(partyChoice.ShowDialog(this) == DialogResult.OK)
			{	partyRow = partyChoice.CurrentRow;
				UpdateControls();
				numericUpDownQntty.Value = partyRow.Qntty;
			}
		}
//-------------------------------------------------------------------------------------------------
	}
}